package com.wuwei.aliocr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.aliocr.domain.Authorization;
import com.wuwei.aliocr.domain.Teacher;
import com.wuwei.aliocr.mapper.AuthorizationMapper;
import com.wuwei.aliocr.mapper.TeacherMapper;
import com.wuwei.aliocr.service.AuthorizationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AuthorizationServiceImpl implements AuthorizationService {

    @Resource
    private AuthorizationMapper authorizationMapper;

    @Resource
    private TeacherMapper teacherMapper;

    /**
     * 根据授权类型查询拥有相应授权的教员
     *
     * @param authorization
     * @return
     */
    @Override
    public List<Teacher> getByAuthorization(String authorization,String teacher) {
        boolean flag = ("M7").equals(authorization)||("M8").equals(authorization);
        if ((("陈靖予").equals(teacher) || ("刘宇航").equals(teacher))&&flag) {
            authorization = authorization+"-PA";
        }
        if ((("吴伟").equals(teacher) || ("陶长海").equals(teacher))&&flag){
            authorization = authorization+"-TA";
        }
        LambdaQueryWrapper<Authorization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(authorization!=null,Authorization::getAuthorization,authorization);
        List<Authorization> authorizations = authorizationMapper.selectList(queryWrapper);
        List<Teacher> list = new ArrayList<>();
        authorizations.forEach(a->{
            LambdaQueryWrapper<Teacher> queryWrapper1 = new LambdaQueryWrapper<>();
            Long teacherId = a.getTeacherId();
            queryWrapper1.eq(teacherId!=null,Teacher::getId,teacherId);
            Teacher selectTeacher = teacherMapper.selectOne(queryWrapper1);
            list.add(selectTeacher);
        });
        return list;
    }

    /**
     * 根据传入的模块名称查询到获得该模块授权的教员
     *
     * @param model
     * @return
     */
    @Override
    public List<Teacher> getAuthorizedTeacher(String model) {
        LambdaQueryWrapper<Authorization> queryWrapper = new LambdaQueryWrapper<>();
        List<Authorization> authorizationList = authorizationMapper.selectList(queryWrapper.eq(model != null, Authorization::getAuthorization, model));
        List<Long> teacherIdList = authorizationList.stream().map(Authorization::getTeacherId).collect(Collectors.toList());
        List<Teacher> teacherList = teacherMapper.selectBatchIds(teacherIdList);
        return teacherList;
    }


}
